package com.example.warpelapp;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class MainActivity extends Activity {

	private ViewFlipper viewFlipper;
	private float lastX;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();

	}

	private void init() {
		viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
		viewFlipper.setDisplayedChild(1);
		
		Button button1 = (Button) findViewById(R.id.button1);

		button1.setOnClickListener(button1ClickListener);
		
		Button button2 = (Button) findViewById(R.id.button2);

		button2.setOnClickListener(button2ClickListener);
		
		Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/digital_7_mono.ttf");
        TextView tv = (TextView) findViewById(R.id.digitalClock1);
        tv.setTextSize(200L);
        tv.setTypeface(tf);

	}

	private OnClickListener button1ClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent browserIntent = new Intent(Intent.ACTION_VIEW,
					Uri.parse("http://warpeles.appspot.com"));
			startActivity(browserIntent);
			
		}
	};
	
	private OnClickListener button2ClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent browserIntent = new Intent(Intent.ACTION_VIEW,
					Uri.parse("http://www.datafutbol.net/comunidad/campeonato/tablas/393"));
			startActivity(browserIntent);
			
		}
	};

	// Method to handle touch event like left to right swap and right to left
	// swap
	public boolean onTouchEvent(MotionEvent touchevent) {
		switch (touchevent.getAction()) {
		// when user first touches the screen to swap
		case MotionEvent.ACTION_DOWN: {
			lastX = touchevent.getX();
			break;
		}
		case MotionEvent.ACTION_UP: {
			float currentX = touchevent.getX();

			// if left to right swipe on screen
			if (lastX < currentX) {
				// If no more View/Child to flip
				if (viewFlipper.getDisplayedChild() == 2)
					break;

				// set the required Animation type to ViewFlipper
				// The Next screen will come in form Left and current Screen
				// will go OUT from Right
				viewFlipper.setInAnimation(this, R.anim.in_from_left);
				viewFlipper.setOutAnimation(this, R.anim.out_to_right);
				// Show the next Screen
				viewFlipper.showNext();
			}

			// if right to left swipe on screen
			if (lastX > currentX) {
				if (viewFlipper.getDisplayedChild() == 0)
					break;
				// set the required Animation type to ViewFlipper
				// The Next screen will come in form Right and current Screen
				// will go OUT from Left
				viewFlipper.setInAnimation(this, R.anim.in_from_right);
				viewFlipper.setOutAnimation(this, R.anim.out_to_left);
				// Show The Previous Screen
				viewFlipper.showPrevious();
			}
			break;
		}
		}
		return false;
	}

}
